package org.ala.tcp_feign_common.handler;

import java.util.HashMap;
import java.util.Map;

import org.ala.tcp_feign_common.enums.TcpFeignMessageTypeEnums;
import org.ala.tcp_feign_common.exceptions.EncodeDecodeException;
import org.ala.tcp_feign_common.logs.LoggerFactory;
import org.ala.tcp_feign_common.msg.AbstractByteMessage;
import org.ala.tcp_feign_common.msg.IByteMessageEncoder;
import org.ala.tcp_feign_common.msg.biz.BizACKByteMessageEncoder;
import org.ala.tcp_feign_common.msg.biz.BizByteMessageEncoder;
import org.ala.tcp_feign_common.msg.biz_resp.BizRespACKByteMessageEncoder;
import org.ala.tcp_feign_common.msg.biz_resp.BizRespByteMessageEncoder;
import org.ala.tcp_feign_common.msg.close.CloseACKByteMessageEncoder;
import org.ala.tcp_feign_common.msg.close.CloseByteMessageEncoder;
import org.ala.tcp_feign_common.msg.heart.HeartACKByteMessageEncoder;
import org.ala.tcp_feign_common.msg.heart.HeartByteMessageEncoder;
import org.ala.tcp_feign_common.msg.login.LoginACKByteMessageEncoder;
import org.ala.tcp_feign_common.msg.login.LoginByteMessageEncoder;
import org.slf4j.Logger;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;


/**
 * 消息物理编码
 * 
 * @author 骆毅(ala)
 * @date 2024年4月18日
 */
public class MessageEncodeHandler extends MessageToByteEncoder<AbstractByteMessage> {

	
	static Logger log = LoggerFactory.getLogger("tcp-feign", "encoder");
	
	
	/**
	 * 消息编码器
	 */
	@SuppressWarnings("rawtypes")
	protected Map<TcpFeignMessageTypeEnums, IByteMessageEncoder> encodeMapper;
	
	public MessageEncodeHandler() {
		init();
	}
	
	/**
	 * 初始化
	 */
	public void init() {
		encodeMapper = new HashMap<>();

		encodeMapper.put(TcpFeignMessageTypeEnums.LOGIN, new LoginByteMessageEncoder());
		encodeMapper.put(TcpFeignMessageTypeEnums.LOGIN_ACK, new LoginACKByteMessageEncoder());
		
		encodeMapper.put(TcpFeignMessageTypeEnums.HEART, new HeartByteMessageEncoder());
		encodeMapper.put(TcpFeignMessageTypeEnums.HEART_ACK, new HeartACKByteMessageEncoder());
		
		encodeMapper.put(TcpFeignMessageTypeEnums.CLOSE, new CloseByteMessageEncoder());
		encodeMapper.put(TcpFeignMessageTypeEnums.CLOSE_ACK, new CloseACKByteMessageEncoder());
		
		encodeMapper.put(TcpFeignMessageTypeEnums.BIZ, new BizByteMessageEncoder());
		encodeMapper.put(TcpFeignMessageTypeEnums.BIZ_ACK, new BizACKByteMessageEncoder());
		
		encodeMapper.put(TcpFeignMessageTypeEnums.BIZ_RESP, new BizRespByteMessageEncoder());
		encodeMapper.put(TcpFeignMessageTypeEnums.BIZ_RESP_ACK, new BizRespACKByteMessageEncoder());
	}
	
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	protected void encode(ChannelHandlerContext ctx, AbstractByteMessage msg, ByteBuf out) throws Exception {
//		log.info("[MessageDecodeHandler decode] 消息编码 ");
		
		//	取消息类型
		IByteMessageEncoder encoder = encodeMapper.get(msg.type());
		if (encoder == null) {
			throw new EncodeDecodeException("未知的消息类型，无法进行编码. type:" + msg.type());
		}
		
		//	执行编码
		encoder.encoding(out, msg);
	}

}
